package com.ruoyi.mes.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.mes.remote.UserFeignClient;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.mes.domain.MesDispatch;
import com.ruoyi.mes.service.IMesDispatchService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 派工单管理Controller
 * 
 * @author ruoyi
 * @date 2021-12-13
 */
@RestController
@RequestMapping("/mes/dispatch")
public class MesDispatchController extends BaseController
{
    @Autowired
    private IMesDispatchService mesDispatchService;


    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 查询派工单管理列表
     */
    @PreAuthorize(hasPermi = "mes:dispatch:list")
    @GetMapping("/list")
    public TableDataInfo list(MesDispatch mesDispatch)
    {
        startPage();
        List<MesDispatch> list = mesDispatchService.selectMesDispatchList(mesDispatch);

        R<LoginUser> usr= userFeignClient.getUserByUsername("admin");
        logger.info("============= " + JSONObject.toJSONString(usr));

        AjaxResult ajaxResult = userFeignClient.getUserByUserId(1);
        logger.info("============= " + JSONObject.toJSONString(ajaxResult));

        return getDataTable(list);
    }

    /**
     * 导出派工单管理列表
     */
    @PreAuthorize(hasPermi = "mes:dispatch:export")
    @Log(title = "派工单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesDispatch mesDispatch) throws IOException
    {
        List<MesDispatch> list = mesDispatchService.selectMesDispatchList(mesDispatch);
        ExcelUtil<MesDispatch> util = new ExcelUtil<MesDispatch>(MesDispatch.class);
        util.exportExcel(response, list, "派工单管理数据");
    }

    /**
     * 获取派工单管理详细信息
     */
    @PreAuthorize(hasPermi = "mes:dispatch:query")
    @GetMapping(value = "/{dispatchUuid}")
    public AjaxResult getInfo(@PathVariable("dispatchUuid") String dispatchUuid)
    {
        return AjaxResult.success(mesDispatchService.selectMesDispatchByDispatchUuid(dispatchUuid));
    }

    /**
     * 新增派工单管理
     */
    @PreAuthorize(hasPermi = "mes:dispatch:add")
    @Log(title = "派工单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesDispatch mesDispatch)
    {
        // 单据编号查重
        MesDispatch d = new MesDispatch();
        d.setDocNum( mesDispatch.getDocNum() );
        List<MesDispatch> listByDocNum = mesDispatchService.selectMesDispatchList(d);
        if(listByDocNum.size()>0){
            return new AjaxResult(1521,"您提交的派工号已存在！请重新填写！");
        }else {
            return toAjax(mesDispatchService.insertMesDispatch(mesDispatch));
//            // 获取当前汇报数量
//            BigDecimal curQty = mesDispatch.getDispatchQty();
//            // 获取剩余可汇报数量
//            BigDecimal remainQty = mesDispatchService.selectRemainDispatchQty(mesDispatch);
//            // 剩余可汇报数量和当前汇报数量做比较
//            if (curQty.compareTo(remainQty) > 0) {
//                return new AjaxResult(1521, "您提交的下达数量为" + curQty + "," + "当前剩余实际可下达数量为" + remainQty + "!");
//            } else {
//
//            }
        }
    }

    /**
     * 修改派工单管理
     */
    @PreAuthorize(hasPermi = "mes:dispatch:edit")
    @Log(title = "派工单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesDispatch mesDispatch)
    {
        return toAjax(mesDispatchService.updateMesDispatch(mesDispatch));
    }

    /**
     * 删除派工单管理
     */
    @PreAuthorize(hasPermi = "mes:dispatch:remove")
    @Log(title = "派工单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dispatchUuids}")
    public AjaxResult remove(@PathVariable String[] dispatchUuids)
    {
        return toAjax(mesDispatchService.deleteMesDispatchByDispatchUuids(dispatchUuids));
    }
}
